package edu.mta.pokerstats;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.cloudfoundry.services.CloudFoundryServices;
import org.cloudfoundry.services.ServiceManager;

public class Team {
	private int m_Id;
	private String m_Name;
	private int m_TeamLeaderId;

	public Team(int i_Id, String i_Name, int i_TeamLeaderId) {
		m_Id = i_Id;
		m_Name = i_Name;
		m_TeamLeaderId = i_TeamLeaderId;
	}

	public Team(ResultSet i_RS) {
		try {
			m_Id = i_RS.getInt("id");
			m_Name = i_RS.getString("name");
			m_TeamLeaderId = i_RS.getInt("teamLeader");
		} catch (SQLException e) {
			new Team(0, "", 0);
		}
	}

	public int getId() {
		return m_Id;
	}

	public void setId(int m_Id) {
		this.m_Id = m_Id;
	}

	public String getName() {
		return m_Name;
	}

	public void setName(String m_Name) {
		this.m_Name = m_Name;
	}

	public int getTeamLeaderId() {
		return m_TeamLeaderId;
	}

	public void setTeamLeaderId(int m_TeamLeaderId) {
		this.m_TeamLeaderId = m_TeamLeaderId;
	}

	public boolean InsertToDatabase() {
		int newId;
		boolean success = false;
		newId = ServiceManager.INSTANCE.InsertQuery(getInsertString());
		if (newId >= 0) {
			m_Id = newId;
			success = true;
		}
		return success;
	}
	
	public boolean DeleteFromDatabase() {
		for (Player player : this.getPlayersInTeam()) {
			player.setTeamId(0);
			player.setJoinStatus("Declined");
			player.UpdateInDatabase();
		}
		String deleteQuery = "DELETE FROM teams WHERE id=" + m_Id;
		ServiceManager.INSTANCE.RunGenerelQuery(deleteQuery);
		return true;
	}

	public boolean UpdateInDatabase() {
		return ServiceManager.INSTANCE.UpdateQuery(getUpdateString());
	}

	private String getInsertString() {
		String result = "insert into teams values(" + null + "," + "'" + m_Name
				+ "'," + m_TeamLeaderId + ")";

		return result;
	}

	private String getUpdateString() {
		String result = "update teams SET " + "name = '" + m_Name + "',"
				+ "teamLeader = " + m_TeamLeaderId;
		result += " WHERE id=" + m_Id;

		return result;
	}
	
	public static ArrayList<Team> getTeamsInSystem(){
		ArrayList<Team> listOfTeams = new ArrayList<Team>();
		Connection connection = null;
		try {
			connection = (Connection) ServiceManager.INSTANCE
					.getInstance(CloudFoundryServices.MYSQL);
			Statement s = connection.createStatement();
			String query = "Select * from teams where id>0 order by id asc;";
			ResultSet rs = s.executeQuery(query);
			while (rs.next()){
				listOfTeams.add(new Team(rs));				
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (connection != null) {
				try {
					if (!connection.isClosed()) {
						connection.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

		return listOfTeams;
	}
	
	public static Team getTeamByName(String i_Name) {
		Team returnValue = null;
		Connection connection = null;
		try {
			connection = (Connection) ServiceManager.INSTANCE
					.getInstance(CloudFoundryServices.MYSQL);
			Statement s = connection.createStatement();
			String query = "Select * from teams where name like '" + i_Name + "'";
			ResultSet rs = s.executeQuery(query);
			rs.first();
			returnValue = new Team(rs);
		} catch (Exception e) {
			returnValue = null;
			e.printStackTrace();
		} finally {
			if (connection != null) {
				try {
					if (!connection.isClosed()) {
						connection.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

		return returnValue;
	}

	public static Team getTeamById(int i_Id) {
		Team returnValue = null;
		Connection connection = null;
		try {
			connection = (Connection) ServiceManager.INSTANCE
					.getInstance(CloudFoundryServices.MYSQL);
			Statement s = connection.createStatement();
			String query = "Select * from teams where id =" + i_Id;
			ResultSet rs = s.executeQuery(query);
			rs.first();
			returnValue = new Team(rs);
		} catch (Exception e) {
			returnValue = null;
			e.printStackTrace();
		} finally {
			if (connection != null) {
				try {
					if (!connection.isClosed()) {
						connection.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

		return returnValue;
	}
	
	public Map<Integer, Game> getGamesOfTeam(){
		HashMap<Integer,Game> gamesMap = new HashMap<Integer,Game>();
		Connection connection = null;
		try {
			connection = (Connection) ServiceManager.INSTANCE
					.getInstance(CloudFoundryServices.MYSQL);
			Statement s = connection.createStatement();
			String query = "Select * from games where teamID=" + m_Id
					+ " order by date desc;";
			ResultSet rs = s.executeQuery(query);
			while (rs.next()) {
				Game tempGame = new Game(rs);
				if (tempGame != null) {
					gamesMap.put(tempGame.getId(), tempGame);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (connection != null) {
				try {
					if (!connection.isClosed()) {
						connection.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return gamesMap;
	}
	
	public ArrayList<Player> getPlayersInTeam() {
		return getPlayersInTeam("Approved");
	}

	public ArrayList<Player> getPlayersInTeam(String i_JoinStatus) {
		ArrayList<Player> playersList = new ArrayList<Player>();
		Connection connection = null;
		try {
			connection = (Connection) ServiceManager.INSTANCE
					.getInstance(CloudFoundryServices.MYSQL);
			Statement s = connection.createStatement();
			String query = "Select * from players where teamID=" + m_Id
					+ " and joinStatus like '" + i_JoinStatus
					+ "' order by name asc;";
			ResultSet rs = s.executeQuery(query);
			while (rs.next()) {
				Player tempUser = new Player(rs);
				if (tempUser != null) {
					playersList.add(tempUser);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (connection != null) {
				try {
					if (!connection.isClosed()) {
						connection.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return playersList;
	}
}